/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rcs.adslmonitor.routers;

import org.rcs.adslmonitor.ADSLStatus.KEYS;
import org.rcs.adslmonitor.ADSLStatusFromHTML;
import org.rcs.adslmonitor.ADSLStatusFromHTML.HttpConnectionRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTMLEditorKit.Parser;
import javax.swing.text.html.parser.ParserDelegator;

/**
 * http://192.168.1.1/SysStatistics_ADSL.html
 *
 * @author pablo
 */
public class ZTEZXV10W300 extends ADSLStatusFromHTML {

  private class HTMLCallback extends HTMLEditorKit.ParserCallback {

    private boolean read = false;
    private String key = null;
    private String value = null;
    private boolean readKey = false;

    private KEYS getKey(String key) {
      for (KEYS currentKey : KEYS.values()) {
        if (currentKey.getKey().startsWith(key)) {
          return currentKey;
        }
      }

      return null;
    }

    @Override
    public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
      super.handleStartTag(t, a, pos);
      if (t == Tag.TABLE && a.getAttributeCount() == 2) {
        read = true;
      } else if (read && t == Tag.TD) {
        if (null == key) {
          readKey = true;
        } else {
          readKey = false;
        }
      }
    }

    @Override
    public void handleEndTag(Tag t, int pos) {
      super.handleEndTag(t, pos);

      if (read) {
        if (t == Tag.TABLE) {
          read = false;
        } else if (t == Tag.TD) {
          if (null != key && null != value) {
            put(getKey(key), value);
            key = null;
            value = null;
          }
        }
      }
    }

    @Override
    public void handleText(char[] data, int pos) {
      super.handleText(data, pos);

      if (read) {
        if (readKey) {
          if (null == key) {
            key = "";
          }

          key += new String(data);
        } else {
          if (null == value) {
            value = "";
          }

          value += new String(data);
        }
      }
    }
  }

  public ZTEZXV10W300() {
    super("ZTE ZXV10 W300");
    
    setUser("admin");
    setPassword("260062EDF52B");
    setRouterURL("http://192.168.1.1");
  }

  private void cleanUnits() {
    cleanUnit(KEYS.DOWN_STREAM);
    cleanUnit(KEYS.ATTAINABLE_DOWN_RATE);
    cleanUnit(KEYS.UP_STREAM);
    cleanUnit(KEYS.ATTAINABLE_UP_RATE);
  }

  @Override
  protected boolean onRefresh() {

    return doConnection("adslconfig.htm", new HttpConnectionRequest() {

      @Override
      public void read(InputStream in) throws Exception {
        BufferedReader rd = new BufferedReader(new InputStreamReader(in, "utf-8"));
        Parser parser = new ParserDelegator();

        parser.parse(rd, new HTMLCallback(), true);

        cleanUnits();
      }
    });
  }
}
